home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / sgml / msdos / sgml07 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  3.0 KB  |  166 lines

  1. /* getopt.c -
  2.    getopt() for those systems that don't have it.
  3.  
  4.    Derived from comp.sources.unix/volume3/att_getopt.
  5.    Modified by James Clark (jjc@jclark.com).
  6. */
  7.  
  8. #include "config.h"
  9.  
  10. #ifndef HAVE_GETOPT
  11.  
  12. #include "std.h"
  13.  
  14. #ifdef SWITCHAR
  15. #include <dos.h>
  16. #endif
  17.  
  18. int    opterr = 1;
  19. int    optind = 1;
  20. int    optopt;
  21. char *optarg;
  22.  
  23. #ifndef OPTION_CHAR
  24. #define OPTION_CHAR '-'
  25. #endif
  26.  
  27. int getopt(argc, argv, opts)
  28. int argc;
  29. char **argv;
  30. char *opts;
  31. {
  32. #ifdef SWITCHAR
  33.     union REGS regs;
  34.     static char switchar = '\0';
  35. #endif
  36.     static int sp = 1;
  37.     register int c;
  38.     register char *cp;
  39.     char *message;
  40. #ifdef SWITCHAR
  41.     if (switchar == '\0') {
  42.         regs.x.ax = 0x3700;
  43.         intdos(®s, ®s);
  44.         if (!regs.x.cflag)
  45.             switchar = regs.h.dl;
  46.         else
  47.             switchar = '/';
  48.     }
  49. #endif
  50.     if (sp == 1) {
  51.         if (optind >= argc)
  52.             return EOF;
  53.         if ((
  54. #ifdef SWITCHAR
  55.             argv[optind][0] != switchar &&
  56. #endif
  57.             argv[optind][0] != OPTION_CHAR) || argv[optind][1] == '\0') {
  58. #ifdef REORDER_ARGS
  59.             int i;
  60.             for (i = optind; i < argc; i++)
  61.                 if ((
  62. #ifdef SWITCHAR
  63.                      argv[i][0] == switchar ||
  64. #endif
  65.                      argv[i][0] == OPTION_CHAR) && argv[i][1] != '\0')
  66.                     break;
  67.             if (i < argc) {
  68.                 c = argv[i][1];
  69. #ifdef CASE_INSENSITIVE_OPTIONS
  70.                 if (isupper(c))
  71.                     c = tolower(c);
  72. #endif
  73.                 if (c != ':' && c != OPTION_CHAR && (cp = strchr(opts, c)) != NULL
  74.                     && cp[1] == ':' && argv[i][2] == 0 && i < argc - 1) {
  75.                     int j;
  76.                     char *temp1 = argv[i];
  77.                     char *temp2 = argv[i+1];
  78.                     for (j = i - 1; j >= optind; j--)
  79.                         argv[j+2] = argv[j];
  80.                     argv[optind] = temp1;
  81.                     argv[optind+1] = temp2;
  82.                 }
  83.                 else {
  84.                     int j;
  85.                     char *temp = argv[i];
  86.                     for (j = i - 1; j >= optind; j--)
  87.                         argv[j+1] = argv[j];
  88.                     argv[optind] = temp;
  89.                 }
  90.             }
  91.             else
  92. #endif
  93.                 return EOF;
  94.         }
  95.         if ((argv[optind][0] == OPTION_CHAR && argv[optind][1] == OPTION_CHAR
  96.                   && argv[optind][2] == '\0')
  97. #ifdef SWITCHAR
  98.             || (argv[optind][0] == switchar && argv[optind][1] == switchar
  99.                 && argv[optind][2] == '\0')
  100. #endif
  101.             ) {
  102.             optind++;
  103.             return(EOF);
  104.         }
  105.     }
  106.     optopt = c = argv[optind][sp];
  107. #ifdef CASE_INSENSITIVE_OPTIONS
  108.     if (
  109. #ifdef USE_ISASCII
  110.         isascii(c) &&
  111. #endif /* USE_ISASCII */
  112.         isupper((unsigned char)c))
  113.         optopt = c = tolower((unsigned char)c);
  114. #endif /* CASE_INSENSITIVE_OPTIONS */
  115.     if (c == ':' || (cp = strchr(opts, c)) == NULL) {
  116.         if (argv[optind][++sp] == '\0') {
  117.             optind++;
  118.             sp = 1;
  119.         }
  120.         message = ": illegal option -- ";
  121.         goto bad;
  122.     }
  123.     if (*++cp == ':') {
  124.         if (argv[optind][sp+1] != '\0')
  125.             optarg = &argv[optind++][sp+1];
  126.         else if (++optind >= argc) {
  127.             sp = 1;
  128.             message = ": option requires an argument -- ";
  129.             goto bad;
  130.         }
  131.         else
  132.             optarg = argv[optind++];
  133.         sp = 1;
  134.     } 
  135.     else {
  136.         if (argv[optind][++sp] == '\0') {
  137.             sp = 1;
  138.             optind++;
  139.         }
  140.         optarg = NULL;
  141.     }
  142.     return c;
  143. bad:
  144.     if (opterr) {
  145.         fputs(argv[0], stderr);
  146.         fputs(message, stderr);
  147.         fputc(optopt, stderr);
  148.         fputc('\n', stderr);
  149.     }
  150.     return '?';
  151. }
  152.  
  153. #endif /* not HAVE_GETOPT */
  154.  
  155. /*
  156. Local Variables:
  157. c-indent-level: 4
  158. c-continued-statement-offset: 4
  159. c-brace-offset: 4
  160. c-argdecl-indent: 4
  161. c-label-offset: -4
  162. tab-width: 4
  163. End:
  164. */
  165.  
  166.